<template>
<div class="container">
  <div class="Main-part">
    <div class="search-container">
      <div class="search-label">智友推荐</div>
      <div class="search-box">
        <input
          type="text"
          class="search-input"
          placeholder="搜索旅游目的地..."
          v-model="searchKeyword"
          @keyup.enter="searchPlans"
        />
        <div class="search-icon" @click="searchPlans">
          <search-outlined />
        </div>
      </div>
    </div>

    <div class="travel-plans-container">
      <div class="travel-plans-grid">
        <div
          v-for="(plan, index) in filteredTravelPlans"
          :key="index"
          class="travel-plan-card"
        >
          <div class="plan-header">
            <div class="plan-number">{{ index + 1 }}</div>
            <div class="plan-title">{{ plan.title }}</div>
          </div>
          <div class="plan-map">
            <img :src="plan.mapImage" alt="旅行路线图" />
          </div>
          <div class="plan-popularity">
            <div class="percentage">{{ plan.popularity }}%</div>
            <div class="popularity-text">初次访问{{ plan.location }}的蜂蜂会选择这条线路</div>
          </div>
          <div class="plan-routes">
            <div v-for="(day, dayIndex) in plan.routes" :key="dayIndex" class="day-route">
              <div class="day-label">D{{ dayIndex + 1 }}</div>
              <div class="route-points">
                <template v-for="(point, pointIndex) in day.points" :key="pointIndex">
                  <span class="route-point">{{ point }}</span>
                  <span v-if="pointIndex < day.points.length - 1" class="route-arrow">→</span>
                </template>
              </div>
            </div>
            <div class="view-more">
              <a href="javascript:void(0)" @click="CheckDetail()">查看></a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <a-button
    v-if="!showForm"
    class="create-plan-btn"
    type="primary"
    @click="toggleForm"
  >
    创建旅行计划
  </a-button>

  <div class="Secondary-part" v-if="showForm">
    <h3 class="form-title">填写关键词生成计划</h3>
    <div class="form-container">
      <div class="form-item">
        <label class="plan-label">地点：</label>
        <a-input v-model:value="formData.location" placeholder="请输入旅游地点" />
      </div>
      <div class="form-item">
        <label class="plan-label">天数：</label>
        <a-input-number v-model:value="formData.days" :min="1" :max="30"  class="white-input"/>
      </div>
      <div class="form-item">
        <label class="plan-label">预算：</label>
        <a-input-number v-model:value="formData.budget" :min="0" :step="1000" addonAfter="元" class="white-input"/>
      </div>
      <div class="form-item">
        <label class="plan-label">人数：</label>
        <a-input-number v-model:value="formData.people" :min="1" :max="20" class="white-input"/>
      </div>
      <div class="form-buttons">
        <a-button type="primary" class="generate-btn" @click="generatePlan">生成计划</a-button>
        <a-button class="cancel-btn" @click="toggleForm">取消</a-button>
      </div>
    </div>
  </div>
</div>
</template>
<script setup>
import { ref, computed, watch } from "vue";
import { message } from 'ant-design-vue';
import { SearchOutlined } from '@ant-design/icons-vue';
import Router from "../router";

// 控制表单显示
const showForm = ref(false);

// 搜索关键词
const searchKeyword = ref('');

// 表单数据
const formData = ref({
  location: '',
  days: 3,
  budget: 5000,
  people: 2
});

// 旅游计划数据
const travelPlans = ref([
  {
    title: '澳门经典2日游',
    location: '澳门',
    mapImage: '/src/assets/img.png',
    popularity: 38,
    routes: [
      {
        points: ['路环岛', '渔仔村']
      },
      {
        points: ['大三巴牌坊', '恋爱巷', '疯堂斜巷', '澳门渔人码头']
      }
    ]
  },
  {
    title: '澳门经典一日游',
    location: '澳门',
    mapImage: '/src/assets/img1.png',
    popularity: 34,
    routes: [
      {
        points: ['大三巴牌坊', '恋爱巷', '疯堂斜巷', '澳门渔人码头']
      }
    ]
  },
  {
    title: '香港经典3日游',
    location: '香港',
    mapImage: '/src/assets/img2.png',
    popularity: 45,
    routes: [
      {
        points: ['维多利亚港', '太平山顶']
      },
      {
        points: ['迪士尼乐园', '海洋公园']
      },
      {
        points: ['尖沙咀', '铜锣湾', '兰桂坊']
      }
    ]
  },
  {
    title: '香港购物2日游',
    location: '香港',
    mapImage: '/src/assets/img3.png',
    popularity: 32,
    routes: [
      {
        points: ['铜锣湾', '旺角', '尖沙咀']
      },
      {
        points: ['中环', '金钟', '海港城']
      }
    ]
  },
  {
    title: '北京文化3日游',
    location: '北京',
    mapImage: '/src/assets/img3.png',
    popularity: 42,
    routes: [
      {
        points: ['故宫', '天安门', '王府井']
      },
      {
        points: ['颐和园', '圆明园', '清华北大']
      },
      {
        points: ['长城', '十三陵', '鸟巢']
      }
    ]
  },
  {
    title: '上海都市2日游',
    location: '上海',
    mapImage: '/src/assets/img.png',
    popularity: 36,
    routes: [
      {
        points: ['外滩', '南京路', '豫园']
      },
      {
        points: ['迪士尼', '陆家嘴', '田子坊']
      }
    ]
  },
  {
    title: '成都美食2日游',
    location: '成都',
    mapImage: '/src/assets/img1.png',
    popularity: 39,
    routes: [
      {
        points: ['宽窄巷子', '锦里', '春熙路']
      },
      {
        points: ['都江堰', '青城山', '大熊猫基地']
      }
    ]
  },
  {
    title: '三亚海滩3日游',
    location: '三亚',
    mapImage: '/src/assets/img2.png',
    popularity: 47,
    routes: [
      {
        points: ['亚龙湾', '大东海']
      },
      {
        points: ['蜈支洲岛', '南山寺']
      },
      {
        points: ['天涯海角', '西岛']
      }
    ]
  },
  {
    title: '丽江古城2日游',
    location: '丽江',
    mapImage: '/src/assets/img.png',
    popularity: 41,
    routes: [
      {
        points: ['大研古城', '四方街', '黑龙潭']
      },
      {
        points: ['玉龙雪山', '蓝月谷', '束河古镇']
      }
    ]
  },
  {
    title: '西安历史3日游',
    location: '西安',
    mapImage: '/src/assets/img3.png',
    popularity: 43,
    routes: [
      {
        points: ['兵马俑', '华清宫']
      },
      {
        points: ['大雁塔', '城墙', '钟鼓楼']
      },
      {
        points: ['华山', '回民街']
      }
    ]
  }
]);

// 过滤后的旅游计划
const filteredTravelPlans = computed(() => {
  if (!searchKeyword.value) {
    return travelPlans.value;
  }

  return travelPlans.value.filter(plan =>
    plan.title.includes(searchKeyword.value) ||
    plan.location.includes(searchKeyword.value)
  );
});

//查看按钮，点击可以查看线路图以及更多攻略信息
const CheckDetail = () =>{
  Router.push('/Home_Page/Introduction_Details')
}

// 切换表单显示状态
const toggleForm = () => {
  showForm.value = !showForm.value;
};

// 搜索旅游计划
const searchPlans = () => {
  if (searchKeyword.value.trim() === '') {
    message.info('请输入搜索关键词');
    return;
  }

  if (filteredTravelPlans.value.length === 0) {
    message.info(`没有找到与"${searchKeyword.value}"相关的旅行计划`);
  }
};

// 生成旅行计划
const generatePlan = () => {
  // 验证表单
  if (!formData.value.location) {
    message.error('请输入旅游地点');
    return;
  }

  if (!formData.value.days) {
    message.error('请输入旅行天数');
    return;
  }

  // 这里添加生成计划的逻辑
  message.success('正在生成旅行计划，请稍候...');

  // TODO: 调用API生成旅行计划
  console.log('生成计划', formData.value);

  // 生成后隐藏表单
  setTimeout(() => {
    toggleForm();
    // 模拟添加新计划
    const newPlan = {
      title: `${formData.value.location}${formData.value.days}日游`,
      location: formData.value.location,
      mapImage: '/src/assets/map_new.jpg',
      popularity: Math.floor(Math.random() * 20) + 30,
      routes: []
    };

    // 根据天数生成路线
    for (let i = 0; i < formData.value.days; i++) {
      newPlan.routes.push({
        points: ['景点A', '景点B', '景点C']
      });
    }

    travelPlans.value.unshift(newPlan);
    message.success('旅行计划已生成！');
  }, 2000);
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.container {
  background-color: #0d0d12; /* 你想要的背景颜色 */
  min-height: 100vh; /* 让整个页面都有颜色 */
}
.Main-part {
  position: absolute;
  top: 15%;
  left: 20%;
  width: 65%;
  height: 80%;
  background-color: #211e2d;
  border-radius: 10px;
  overflow-y: auto; /* Enable scrolling for overflow */
  padding: 0;
  box-sizing: border-box;
}

.search-container {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background-color: #211e2d;
  padding: 20px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
}

.travel-plans-container {
  padding: 0 20px 20px 20px;
}

.search-label {
  color: #aa00aa;
  font-size: 18px;
  font-weight: bold;
  margin-right: 15px;
}
.plan-label{
  color: #baff00 !important;
  font-weight: bolder;
}
.ant-input::placeholder {
  color: white;
}
.white-input :deep(.ant-input-number-input) {
  color: white;
  background-color: transparent; /* 可选：让背景更统一 */
}

.white-input :deep(.ant-input-number-group-addon) {
  color: white;
  background-color: transparent;
}
.search-box {
  position: relative;
  width: 50%;
  max-width: 600px;
}

.search-input {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  background-color: transparent;
  border: none;
  border-bottom: 2px solid rgba(170, 0, 170, 0.5);
  color: #fff;
  outline: none;
  transition: all 0.3s;
}

.search-input:focus {
  border-bottom: 2px solid #aa00aa;
}

.search-icon {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #aa00aa;
  font-size: 20px;
  cursor: pointer;
}

.travel-plans-container {
  margin-top: 20px;
}

.travel-plans-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.travel-plan-card {
  background-color: rgba(49, 47, 62, 0.8);
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(170, 0, 170, 0.3);
  transition: all 0.3s;
}

.travel-plan-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(170, 0, 170, 0.3);
}

.plan-header {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  background-color: #fff;
}

.plan-number {
  width: 30px;
  height: 30px;
  background-color: #ff9800;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  border-radius: 5px;
  margin-right: 10px;
}

.plan-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.plan-map {
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.plan-map img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.plan-popularity {
  padding: 15px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(170, 0, 170, 0.2);
}

.percentage {
  font-size: 24px;
  font-weight: bold;
  color: #ff9800;
  margin-right: 10px;
}

.popularity-text {
  font-size: 14px;
  color: #ddd;
}

.plan-routes {
  padding: 15px;
  color: #fff;
}

.day-route {
  display: flex;
  margin-bottom: 10px;
}

.day-label {
  font-weight: bold;
  margin-right: 10px;
  color: #aa00aa;
}

.route-points {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.route-point {
  margin-right: 5px;
}

.route-arrow {
  margin: 0 5px;
  color: #aa00aa;
}

.view-more {
  text-align: right;
  margin-top: 10px;
}

.view-more a {
  color: #aa00aa;
  text-decoration: none;
}

.create-plan-btn {
  position: absolute;
  top: 15%;
  left: 4%;
  width: 15%;
  background: linear-gradient(45deg, #aa00aa, #5500aa);
  border: none;
  height: 50px;
  font-weight: bold;
  font-size: 16px;
  transition: all 0.3s;
}

.create-plan-btn:hover {
  box-shadow: 0 0 10px rgba(170, 0, 170, 0.8);
  transform: translateY(-2px);
}

.Secondary-part{
  position: absolute;
  top: 15%;
  left: 4%;
  width: 15%;
  height: auto;
  min-height: 30%;
  background-color: #211e2d;
  border-radius: 10px;
  overflow-y: auto;
  padding: 20px;
  box-sizing: border-box;
  color: #fff;
  z-index: 10;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

/* 表单样式 */
.form-title {
  text-align: center;
  margin-bottom: 20px;
  color: #fff;
  font-size: 18px;
  background: linear-gradient(45deg, #aa00aa, #5500aa);
  padding: 10px;
  border-radius: 6px;
}

.form-container {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-item label {
  font-size: 14px;
  color: #dddddd;
}

.form-buttons {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.generate-btn {
  flex: 1;
  background: linear-gradient(45deg, #aa00aa, #5500aa);
  border: none;
  height: 40px;
  font-weight: bold;
  transition: all 0.3s;
}

.cancel-btn {
  flex: 1;
  background: transparent;
  border: 1px solid #aa00aa;
  color: #fff;
  height: 40px;
  transition: all 0.3s;
}

.generate-btn:hover, .cancel-btn:hover {
  box-shadow: 0 0 10px rgba(170, 0, 170, 0.8);
  transform: translateY(-2px);
}

/* 自定义 ant-design-vue 组件样式 */
:deep(.ant-input),
:deep(.ant-input-number) {
  background-color: rgba(33, 30, 45, 0.8);
  border: 1px solid rgba(170, 0, 170, 0.3);
  color: #fff;
}

:deep(.ant-input-number-handler-wrap) {
  background-color: rgba(33, 30, 45, 0.8);
}

:deep(.ant-input-number-handler) {
  color: #aaa;
}

:deep(.ant-input:focus),
:deep(.ant-input-number:focus) {
  border-color: #aa00aa;
  box-shadow: 0 0 0 2px rgba(170, 0, 170, 0.2);
}
</style>
